import sys
import pygame

from shezhi import Shezhi
from huojian import Huojian
from bullet import Bullet
class RocketGo:
    '''管理游戏资源和行为'''
    def __init__(self):
        pygame.init()
        self.shezhi=Shezhi()
        #实例化火箭
        self.pingmu=pygame.display.set_mode(
            (self.shezhi.pingmu_width,self.shezhi.pingmu_height))
        pygame.display.set_caption('火箭练习')
        self.huojian = Huojian(self)

        self.bullets = pygame.sprite.Group()

        self.enemy = pygame.sprite.Group()
    def run_fame(self):
        while True:
            self._check_events()
            self.huojian.update()
            self.bullets.update()
            self._update_screen()

    def _check_events(self):
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    self._check_dow(event)

                elif event.type == pygame.KEYUP:
                    self._check_up(event)

    def _check_dow(self,event):             #按下键盘
        if event.key == pygame.K_RIGHT:  # 右键持续移动
            self.huojian.biaozhi_right = True
        elif event.key == pygame.K_LEFT:
            self.huojian.biaozhi_left = True
        elif event.key == pygame.K_UP:
            self.huojian.biaozhi_up = True
        elif event.key == pygame.K_DOWN:
            self.huojian.biaozhi_dow = True

        elif event.key == pygame.K_q:           #按下q键就会关闭程序
            sys.exit()

        elif event.key == pygame.K_SPACE: #按下空格发射子弹
            self._fire_bullet()

    def _check_up(self,event):              #松开键盘
        if event.key == pygame.K_RIGHT:
            self.huojian.biaozhi_right = False
        elif event.key == pygame.K_LEFT:
            self.huojian.biaozhi_left = False
        elif event.key == pygame.K_UP:
            self.huojian.biaozhi_up = False
        elif event.key == pygame.K_DOWN:
            self.huojian.biaozhi_dow = False

    def _fire_bullet(self):
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)

    def _update_screen(self):
        self.pingmu.fill(self.shezhi.color)
        self.huojian.huizhi()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

        pygame.display.flip()




if __name__=='__main__':
    hj=RocketGo()
    hj.run_fame()